/**
 * 任务管理 - 我的任务列表
 * 版本号： v1.0.0
 */
$(function () {


    $("#myTask_List").datagrid({
        title: "我的采购任务列表",
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        striped:true,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/task/myTaskList.do',
        idField: 'id',
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 160, fixed: true},
                {field: 'coName', title: '合同名称', width: 160, fixed: true},
                {field: 'coNumber', title: '合同编号', width: 140, fixed: true},
                {field: 'purCode', title: '采购单号', width: 170, fixed: true},
                {field: 'purPerson', title: '请购人', width: 70, fixed: true, align:'center'},
                {field: 'name', title: '当前步骤', width: 120, fixed: true, align:'center'},
                {field: 'assignee', title: '办理人', width: 70, fixed: true, align:'center'},
                {field: 'create_time', title: '创建时间', width: 135}
            ]
        ],
        onDblClickRow : function(){
            toEdit_mytaskForm();
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                $("#myTask_List").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#myTask_Menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });




});




/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //编辑按钮(办理任务)
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            toEdit_mytaskForm();
                        }
                    });
                }
                //查看按钮(查看当前流程图)
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            toView_myTaskImage();
                        }
                    });
                }

            }
        }
    }
    return toolbar;
}




/**
 * 调整至办理任务（编辑）
 */
function toEdit_mytaskForm(){
    var row = $("#myTask_List").datagrid("getSelected");
    if (null == row) {
        $.messager.show({title: '操作提示', msg: "请选择查看采购申请!", timeout: 3000, showType: "slide"});
        return;
    }
    var id = row.id;
    var width = window.parent.window.document.body.offsetWidth;
    var height = window.parent.window.document.body.offsetHeight;
    var url = CONTEXT_PATH + "/task/toTaskForm.do?id=" + id;
    var option = {
        title : "采购任务办理",
        iconCls : "icon-edit",
        width : width,
        height : height,
        closable : true,
        style : {"display" : "block"},
        top:1,
        left:1,
        closed : false,
        minimizable: false,
        maximizable: false,
        collapsible: false,
        content : "<iframe id='taskIframe' scrolling = 'yes' frameborder = '0'  src='" + url + "' style='width:100%;height:100%'></iframe>"
    }
    window.parent.openTaskWindow(option);
}






/**
 * 查看当前任务流程图(查看)
 */
function toView_myTaskImage(){
    var row = $("#myTask_List").datagrid("getSelected");
    if (null == row) {
        $.messager.show({title: '操作提示', msg: "请选择查看任务!", timeout: 3000, showType: "slide"});
        return;
    };
    $("#showCurrentView").dialog({
        title: "查看当前流程图",
        iconCls: "",
        width: window.document.body.offsetWidth,
        height: window.document.body.offsetHeight,
        cache: false,
        modal: true,
        href: CONTEXT_PATH + "/task/viewCurrentImage.do?taskId=" + row.id
    }).show();
}
